#if __has_include(<SEGMasonry/SEGMasonry.h>)
#import <SEGMasonry/SEGMasonry.h>
#else
#import "SEGMasonry.h"
#endif

#if __has_include(<SEGTool/UIColor+SEGCategory.h>)
#import <SEGTool/UIColor+SEGCategory.h>
#else
#import "UIColor+SEGCategory.h"
#endif

#import "SEGRootView.h"

@interface SEGRootView()
@end

@implementation SEGRootView

#pragma mark - life cycle
- (instancetype)init {
    if (self == [super init]) {
        [self segInitViews];
        [self bindViewModel];
        [self updateConstraints];
    }
    return self;
}

- (instancetype)initWithViewModel:(SEGRootViewModel*)viewModel{
    if (self == [super init]) {
        _viewModel = viewModel;
        [self segInitViews];
        [self updateConstraints];
        [self bindViewModel];
    }
    return self;
}

-(void)dealloc {
    NSLog(@"%s dealloc",__FUNCTION__);
}

#pragma mark - Init Views
-(void)segInitViews{
    self.backgroundColor = [UIColor whiteColor];

}

#pragma mark - Layout
- (void)updateConstraints {
    [super updateConstraints];

}

#pragma mark - Public Methods
#pragma mark - Private Methods
#pragma mark - RAC Data Binding
- (void)bindViewModel {
    //@weakify(self);

    // {@"code":@(0),@"msg":@""}
    [self.viewModel.messageSubject subscribeNext:^(NSDictionary*  _Nullable x) {
        //@strongify(self);
        NSInteger code = [[x valueForKey:@"code"] integerValue];
        NSString *msg = [x valueForKey:@"msg"];

        switch(code) {
            case 0: 
            NSLog(@"%@",msg);
            break;
            default:
                break;
        }
    }];

    [[[self.viewModel.requestCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];

    [self.viewModel.requestCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];

    // [self.viewModel.requestCommand execute:nil];

}
#pragma mark - getter

-(SEGRootViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGRootViewModel alloc]init];
    }
    return _viewModel;
}
#pragma mark setter
@end
